/**
 ****************************************************************************************************
 * @file        freertos.c
 * @author      正点原子团队(ALIENTEK)
 * @version     V1.4
 * @date        2022-01-04
 * @brief       FreeRTOS 移植实验
 * @license     Copyright (c) 2020-2032, 广州市星翼电子科技有限公司
 ****************************************************************************************************
 */

#include "userConfig.h"
#include "usart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

/* START_TASK 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define START_TASK_PRIO         1
#define START_TASK_STACK_SIZE   256
static TaskHandle_t    start_task_handler;

/* TASK1 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define TASK1_PRIO         2
#define TASK1_STACK_SIZE   256
static TaskHandle_t    task1_handler;



/* TASK2 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define TASK2_PRIO         3
#define TASK2_STACK_SIZE   256

static TaskHandle_t    task2_handler;

static QueueHandle_t count_semphore_handle;


/* 任务一，释放计数型信号量 */
static void task1(void * pvParameters)
{
    uint8_t key = 0;

    while(1)
    {
        if(key == 0)
        {
            if(count_semphore_handle != NULL)
            {
                /* 释放信号量 */
                xSemaphoreGive(count_semphore_handle);
            }
        }
        vTaskDelay(500);
    }
}

/* 任务二，获取计数型信号量 */
static void task2(void * pvParameters)
{
    BaseType_t err = 0;

    while(1)
    {
        /* 获取信号量并死等 */
        err = xSemaphoreTake(count_semphore_handle, portMAX_DELAY);
        if(err == pdTRUE)
        {
            usart_transmit("信号量的计数值为：%d\r\n",(int)uxSemaphoreGetCount(count_semphore_handle));
        }
        vTaskDelay(1000);
    }
}

static void start_task(void * pvParameters)
{
    taskENTER_CRITICAL();
    xTaskCreate((TaskFunction_t         )   task1,
                (char *                 )   "task1",
                (configSTACK_DEPTH_TYPE )   TASK1_STACK_SIZE,
                (void *                 )   NULL,
                (UBaseType_t            )   TASK1_PRIO,
                (TaskHandle_t *         )   &task1_handler );

    xTaskCreate((TaskFunction_t         )   task2,
                (char *                 )   "task2",
                (configSTACK_DEPTH_TYPE )   TASK2_STACK_SIZE,
                (void *                 )   NULL,
                (UBaseType_t            )   TASK2_PRIO,
                (TaskHandle_t *         )   &task2_handler );

    vTaskDelete(NULL);

    taskEXIT_CRITICAL();
}

void semaphore_counting_demo(void)
{
    count_semphore_handle = xSemaphoreCreateCounting(100 , 0);
    if(count_semphore_handle != NULL)
    {
        usart_transmit("计数型信号量创建成功！！！\r\n");
    }
    xTaskCreate((TaskFunction_t         )   start_task,
                (char *                 )   "start_task",
                (configSTACK_DEPTH_TYPE )   START_TASK_STACK_SIZE,
                (void *                 )   NULL,
                (UBaseType_t            )   START_TASK_PRIO,
                (TaskHandle_t *         )   &start_task_handler);
    vTaskStartScheduler();
}
